/** @namespace app.model.Platform */
module.exports = app => {
    const {STRING} = app.Sequelize;

    const Platform = app.model.define('platform', {

        name: {type: STRING, unique: 'name', allowNull: false,},
        platform: {type: STRING, unique: 'platform'},//iresearch 用的
        type: {type: STRING, unique: 'type'},//小葫芦用饿的
        url: {type: STRING},
    }, {timestamps: false});

    Platform.associate = () => {
        const {Platform, DailyCategory, DailyPlatformCategory, DailyCategoryData, Report, ReportImage} = app.model;

        Platform.belongsToMany(DailyCategory, {
            as: 'category',
            through: {
                model: DailyPlatformCategory,
                unique: false,
                foreignKey: 'platform_id'
            },
        });

        DailyCategory.belongsToMany(Platform, {
            through: {
                model: DailyPlatformCategory,
                unique: false,
                foreignKey: 'category_id'
            },
        });

        DailyPlatformCategory.belongsTo(Platform);
        DailyPlatformCategory.belongsTo(DailyCategory);

        Platform.hasMany(DailyPlatformCategory);
        DailyCategory.hasMany(DailyPlatformCategory);

        DailyPlatformCategory.hasMany(DailyCategoryData, {as: 'data'});

        Platform.hasMany(DailyCategoryData, {as: 'data'});
        DailyCategory.hasMany(DailyCategoryData, {as: 'data'});

        DailyCategoryData.belongsTo(Platform);
        DailyCategoryData.belongsTo(DailyCategory);

        const {DetailAnchor, DetailAnchorSchedule, DetailScheduleImage, Project, File} = app.model;

        DetailAnchor.belongsTo(Platform, {
            foreignKey: {name: 'platform_id', unique: 'anchor_and_platform'}
        });

        Platform.hasMany(DetailAnchor, {
            as: 'anchors',
            foreignKey: {name: 'platform_id', unique: 'anchor_and_platform'}
        });


        //图片与平台
        Platform.belongsToMany(File, {
            as: 'images', foreignKey: 'platform_id', through: {model: DetailScheduleImage, unique: false,},
        });
        File.belongsToMany(Platform, {
            through: {model: DetailScheduleImage, unique: false,},
        });


        //图片与直播记录===========
        DetailAnchorSchedule.belongsToMany(File, {
            as: 'images', foreignKey: 'schedule_id', through: {
                model: DetailScheduleImage, unique: false, scope: {anchor_id: {[app.Sequelize.Op.ne]: null}},
            },
        });
        File.belongsToMany(DetailAnchorSchedule, {
            as: 'schedule', through: {model: DetailScheduleImage, unique: false,},
        });
        File.hasOne(DetailScheduleImage, {as: 'scheduleImage', foreignKey: 'file_id'});
        DetailScheduleImage.belongsTo(File);
        //每个直播记录有一个硬广截图
        DetailAnchorSchedule.belongsTo(File, {as: 'image', foreignKey: 'image_id'});


        //图片与日志，用于邮件中显示图片=========
        Report.belongsToMany(File, {
            as: 'files', foreignKey: 'report_id', through: {model: ReportImage, unique: false,},
        });
        File.belongsToMany(Report, {
            as: 'report', through: {model: ReportImage},
        });
        File.hasOne(ReportImage);
        ReportImage.belongsTo(File);


        //图片与主播===========
        DetailAnchor.belongsToMany(File, {
            as: 'images', foreignKey: 'anchor_id', through: {model: DetailScheduleImage, unique: false,},
        });
        File.belongsToMany(DetailAnchor, {
            as: 'anchor', through: {model: DetailScheduleImage, unique: false,},
        });
        DetailScheduleImage.belongsTo(DetailAnchor, {as: 'anchor'});


        DetailAnchorSchedule.belongsTo(DetailAnchor, {as: 'anchor', foreignKey: 'anchor_id'});
        DetailAnchorSchedule.belongsTo(Platform, {foreignKey: 'platform_id'});
        DetailAnchorSchedule.belongsTo(Project, {foreignKey: 'project_id'});

    };

    return Platform;
};